from bson import ObjectId

import arrow
import datetime


def trans_datetime(data):
    return arrow.get(data).isoformat()


def trans_id(data):
    return str(data)


METHOD_MAP = {
    datetime.datetime: trans_datetime,
    ObjectId: trans_id
}


class SimpleSerializer(object):
    def __init__(self, data, allow_keys=None, many=False):
        self.data = {}

        if data:
            if hasattr(data, 'to_mongo'):
                data = data.to_mongo()
            if hasattr(data, 'to_dict'):
                data = data.to_dict()
            for k, v in data.items():
                if (allow_keys and k not in allow_keys) and (k != '_id' or 'id' not in allow_keys):
                    continue

                if func := METHOD_MAP.get(type(v)):
                    self.data[k] = func(v)
                else:
                    self.data[k] = v
